<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item,index) in nameList" :key="item.roleId" class="role-item" :class="{active:myIndex===index}" @click="roleClick(item.roleId,index)">
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          {{ item.roleName }}

        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editRole(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/addRole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <div class="tree-wrapper">
        <div v-for="item in list" :key="item.id" class="tree-item">
          <div class="tree-title"> {{ item.title }} </div>
          <el-tree
            ref="tree"
            :data="item.children"
            show-checkbox
            :props="defaultProps"
            disabled
            node-key="id"
            default-expand-all
            :default-checked-keys="dataList"
            check-strictly
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getRolePermissionAPI, getAllPermissionAPI } from '@/api/roleUser'
export default {
  name: 'Role',
  data() {
    return {
      myIndex: 0,
      // 角色列表
      nameList: [],
      // 权限列表
      dataList: [],
      // 全部权限列表
      list: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  async mounted() {
    // 获取角色列表
    await this.getRoleList()
    // 获取权限信息
    await this.getAllPermission()
    // 默认获取第一个角色权限信息
    await this.getRolePermission(2)
  },
  methods: {
    // 封装获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI()
      this.nameList = res.data
    },
    // 封装获取权限信息
    async getRolePermission(id) {
      const res = await getRolePermissionAPI(id)
      this.dataList = res.data.perms
      // 获取tree组件
      const treeList = this.$refs.tree
      // 设置tree组件选中状态
      treeList.forEach((item, index) => {
        item.setCheckedKeys(this.dataList[index])
      })
    },
    // 角色点击事件,获取权限信息
    async roleClick(id, index) {
      this.myIndex = index
      const roleId = this.nameList[index].roleId
      this.getRolePermission(roleId)
    },
    // 查询权限信息
    async getAllPermission() {
      const res = await getAllPermissionAPI()
      this.list = res.data
      this.addTree(this.list)
    },
    // 设置tree,禁止选择
    addTree(tree) {
      tree.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addTree(item.children)
        }
      })
    },
    // 编辑角色
    editRole(id) {
      console.log(id)
      this.$router.push({
        path: '/addRole',
        query: {
          id
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
   .custom-icon{
      fill: #4770ff;
    }
  }
}
</style>
